home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c1.zip / DUMP2.C < prev    next >
Text File  |  1987-06-18  |  6KB  |  185 lines

  1.  
  2. /*  dump.c  (10/83)  Debug style dump of a file from any starting position.
  3.  
  4.  
  5.   Usage: dump filespec [block [page]] | [segment:[offset]] [count]
  6.          Where a block is 64K bytes and a page is 256 bytes.
  7.          Segment:offset are standard 8086 notation in hexadecimal.
  8.          Count is the number of bytes to dump in decimal.
  9.  
  10.    dump b:\calc\data.dbm 1 16
  11.         Positions file to absolute offset of 1000:1000 or 69632.
  12.    dump bigfile :4F0 256
  13.         Positions file to absolute offset of 4F0H or 1264 and
  14.         dumps 256 bytes.
  15.  
  16.      The original source for dump was found on Lynn Long's BBS in Tulsa.  I
  17. modified it to include file positioning.  It has proven to be a great tool at
  18. times when I have to work with binary files that often exceed several million
  19. characters.
  20.    I have been using the Lattice 'C' compiler for about a year now and have
  21. been very pleased with it.  I have re-written the level 1 I/O routines to take
  22. advantage of DOS 2.0 path names, etc. and modified the _main module to allow
  23. me to load and execute programs from within 'c'.
  24.  
  25.                         Ted Reuss     c/o South Texas Software, Inc.
  26.                   Home: 713/961-3926      4544 Post Oak Place, Suite 176
  27.                   Offi: 713/877-8205      Houston, Tx 77027
  28.  
  29. */
  30. #include <stdio.h>
  31. #include <ctype.h>              /* Lattice character type macros */
  32.  
  33. #define FAIL        1
  34. #define SUCCESS     0
  35. #define TRUE      (-1)
  36. #define FALSE       0
  37. #define NULL        0           /* null pointer value */
  38. #define FOREVER     for (;;)
  39. #define PAUSE       if (getch()=='\0') getch();
  40. #define STDIN       0
  41. #define STDOUT      1
  42. #define STDERR      2
  43.  
  44. #define BUFSIZE 512
  45. typedef unsigned    ushort;
  46. typedef int         void;
  47.  
  48. int _fmode = 0x8000;            /* Lattice 'c' - forces binary I/O */
  49. extern short errno;             /* DOS 2.0 error number */
  50. long lseek();
  51.  
  52. char   buffer[BUFSIZE];         /* input buffer */
  53. ushort block = 0;               /* block number ( 64k bytes/block ) */
  54. ushort page = 0;                /* page number ( 256 bytes/page ) */
  55. ushort segment = 0;
  56. ushort offset = 0;
  57. long   filpos = 0;              /* beginning file position */
  58. long   count = 0x7FFFFFFFL;     /* number of bytes to dump */
  59.  
  60. main(argc,argv)                 /* DUMP ENTRY */
  61. int  argc;
  62. char *argv[];
  63. {
  64.    char c;
  65.    ushort i, numin, tot, file, cfrom, flag = 0;
  66.  
  67.    if (argc < 2)
  68.       abort( "Usage:dump filespec [block [page]] | [seg:[ofs]]", 0, 0 );
  69.  
  70.    if ((file = open( argv[1], 0 )) == -1)
  71.       abort( "cannot open", argv[1], errno );
  72.  
  73.    if (argc > 2) {
  74.       if ((flag = stpchr( argv[2], ':' )) != NULL) {
  75.          i = stch_i( argv[2], &segment );
  76.          stch_i( argv[2]+i+1, &offset );
  77.       }
  78.       if (sscanf( argv[2], "%d", &block ) != 1)
  79.          abort( "invalid block", argv[2], 0 );
  80.    }
  81.    if (argc > 3)
  82.       if (sscanf( argv[3], "%d", &page ) != 1)
  83.          abort( "invalid page", argv[3], 0 );
  84.  
  85.    if ( flag ) {
  86.       filpos = (long)segment*16L + (long)offset;
  87.       tot = offset;
  88.    }
  89.    else {
  90.       filpos = (block * 65536L) + (page * 256);
  91.       tot = page * 256;
  92.       segment = block * 4096;
  93.    }
  94.  
  95.    if (lseek( file, filpos, 0 ) == -1L)
  96.       abort( "positioning to", argv[2], errno );
  97.  
  98.    if (argc > 4)
  99.       if (sscanf( argv[4], "%ld", &count ) != 1)
  100.          abort( "invalid count", argv[4], 0 );
  101.  
  102.  
  103.    do {                                    /* read & dump BUFSIZE bytes */
  104.       numin = read( file, buffer, BUFSIZE );
  105.       if (numin == -1)
  106.          abort( "cannot read", argv[1], errno );
  107.       cfrom=0;
  108.       while (cfrom < numin) {
  109.  
  110.          ohw(segment);                     /* print offset in hex */
  111.          putchar(':');
  112.          ohw(cfrom+tot);
  113.          putchar(' ');
  114.  
  115.          for (i=0; i < 16; i++) {          /* print 16 bytes in hex */
  116.             putchar(' ');
  117.             ohb(buffer[cfrom++]);
  118.          }
  119.  
  120.          putchar(' '); putchar(' '); putchar(' ');
  121.  
  122.          cfrom -= 16;
  123.          for (i=0; i < 16; i++) {          /* print 16 bytes in ASCII */
  124.             c = buffer[cfrom] & 0x7f;
  125.             if ( isprint(c) )              /* if printable character */
  126.                putchar(c);
  127.             else
  128.                putchar('.');               /* else print period */
  129.             cfrom++;
  130.          }
  131.  
  132.          putchar('\r'); putchar('\n');     /* print CR/LF */
  133.  
  134.          if ((count -= 16) <= 0)             /* is count exhausted? */
  135.             exit(0);
  136.       }                                    /* end of while */
  137.       tot += numin;
  138.       if ( tot == 0 )
  139.          segment += 4096;
  140.    }                                       /* end of do */
  141.    while (numin == BUFSIZE);
  142. }                                          /* end of main */
  143.  
  144. void ohw(wrd)                        /*      print a word in hex     */
  145. ushort wrd;
  146. {
  147.    ohb( wrd>>8 );
  148.    ohb( wrd );
  149. }
  150.  
  151. void ohb(byt)                        /*      print a byte in hex     */
  152. char byt;
  153. {
  154.    onib( byt>>4 );
  155.    onib( byt );
  156. }
  157.  
  158. void onib(nib)                   /*      print a nibble as a hex character   */
  159. char nib;
  160. {
  161.    nib &= 15;
  162.    putchar((nib >= 10) ? nib-10+'A': nib+'0');
  163. }
  164.  
  165. void abort( msg1 ,msg2 ,errno)     /*  print error msg1, msg2, and nbr */
  166. char *msg1,*msg2;                  /*   Does not close files.  */
  167. short errno;
  168. {
  169.    char stemp[10];
  170.  
  171.    write( STDERR, "ERR: ", 5 );
  172.    if (msg1)
  173.       write( STDERR, msg1, strlen(msg1) );
  174.    if (msg2)
  175.       write( STDERR, " ", 1 );
  176.       write( STDERR, msg2, strlen(msg2) );
  177.    if (errno)   {
  178.       sprintf( stemp," #%d", errno );
  179.       write( STDERR, stemp, strlen(stemp) );
  180.    }
  181.    write( STDERR, "\r\n", 2 );
  182.    _exit( FAIL );
  183. }
  184. /** END DUMP **/
  185.